﻿using GuardMonitor.Model;
using GuardMonitor.ViewModel;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 新门禁.Utils;

namespace 门禁系统.ViewModel
{
    public class MainViewModel:RecordViewModel
    {
        public ObservableCollection<Record> _inList = new ObservableCollection<Record>();
        public ObservableCollection<Record> _inList2 = new ObservableCollection<Record>();
        public ObservableCollection<Record> _outList = new ObservableCollection<Record>();
        public ObservableCollection<Record> _outList2 = new ObservableCollection<Record>();


        public ObservableCollection<Record> carInList
        {
            get { return _inList; }
            set
            {
                _inList = value;
                RaisePropertyChanged("carInList");
            }
        }
        public ObservableCollection<Record> carInList2
        {
            get { return _inList2; }
            set
            {
                _inList2 = value;
                RaisePropertyChanged("carInList2");
            }
        }

        public ObservableCollection<Record> carOutList
        {
            get { return _outList; }
            set
            {
                _outList = value;
                RaisePropertyChanged("carOutList");
            }
        }
        public ObservableCollection<Record> carOutList2
        {
            get { return _outList2; }
            set
            {
                _outList2 = value;
                RaisePropertyChanged("carOutList2");
            }
        }


        public MainViewModel()
        {
            //carInList.Add(new Record() { xh = "1",invName = "石油焦", carNo = "鲁E06A55", cTime = "2020-12-09 15:33:12", orderNo = "A12020120900001" });
        }

        public void add()
        {
            carInList.Add(new Record() { xh = (carInList.Count + 1) + "", invName = "石油焦", carNo = "鲁E06A55", cTime = "2020-12-09 15:33:12", orderNo = "A12020120900001" });
            carInList2.Add(new Record() { xh = (carInList.Count + 1) + "", invName = "石油焦", carNo = "鲁E06A55", cTime = "2020-12-09 15:33:12", orderNo = "A12020120900001" });
            carOutList.Add(new Record() { xh = (carInList.Count + 1) + "", invName = "石油焦", carNo = "鲁E06A55", cTime = "2020-12-09 15:33:12", orderNo = "A12020120900001" });
            carOutList2.Add(new Record() { xh = (carInList.Count + 1) + "", invName = "石油焦", carNo = "鲁E06A55", cTime = "2020-12-09 15:33:12", orderNo = "A12020120900001" });
        }
        public void LoadData(string obj, string flag)
        {
            try
            {
                
                JObject jsonObj = JObject.Parse(obj);
                bool success = (bool)jsonObj.GetValue("success");
                if (!success) return;
                int idx = 0;
                carInList.Clear();
                JObject dataObj = (JObject)jsonObj.GetValue("data");
                JArray waitInArray = (JArray)dataObj.GetValue("willInList");
                //String check= (String)jsonObj.GetValue("check");
                
                
                idx = 0;
                foreach (JObject item in waitInArray)
                {
                    idx++;
                    Record record = getRecordByJSON(item);
                    record.xh = idx.ToString();
                    if (flag.Equals("1"))
                    {
                        record.Visibility = "Visible";
                    }
                    else
                    {
                        record.Visibility = "Hidden";
                    }

                    if (flag.Equals("1"))
                    {
                        record.checkVisibility = "Visible";
                    }
                    else
                    {
                        record.checkVisibility = "Hidden";

                    }
                    carInList.Add(record);
                }

                carInList2.Clear();
                idx = 0;
                JArray hasInArray = (JArray)dataObj.GetValue("inList");
                foreach (JObject item in hasInArray)
                {
                    idx++;
                    Record record = getRecordByJSON(item);
                    record.xh = idx.ToString();
                    carInList2.Add(record);
                }

                carOutList.Clear();
                idx = 0;
                JArray waitOutArray = (JArray)dataObj.GetValue("willOutList");
                foreach (JObject item in waitOutArray)
                {
                    idx++;
                    Record record = getRecordByJSON(item);
                    record.xh = idx.ToString();
                    if (flag.Equals("1"))
                    {
                        record.Visibility = "Visible";
                    }
                    else
                    {
                        record.Visibility = "Hidden";
                    }
                    carOutList.Add(record);
                }

                carOutList2.Clear();
                idx = 0;
                JArray hasOutArray = (JArray)dataObj.GetValue("outList");
                foreach (JObject item in hasOutArray)
                {
                    idx++;
                    Record record = getRecordByJSON(item);
                    record.xh = idx.ToString();
                    carOutList2.Add(record);
                }
            }
            catch (Exception ex) {
                LogObject.WriteLog("send message: " , "loadData");

            }
        }
        public Record getRecordByJSON(JObject item)
        {
            Record record = new Record();
            record.id = (String)item.GetValue("id");
            record.xh = (String)item.GetValue("xh");
            record.carNo = (String)item.GetValue("carNo");
            record.invName = (String)item.GetValue("invName");
            record.orderNo = (String)item.GetValue("orderNo");
            record.cTime = (String)item.GetValue("cTime");
            
                String status = (String)item.GetValue("checkStatus");
                if (status.Equals("0"))
                {
                    record.CheckStatus = "待安检";
                }
                else if (status.Equals("1"))
                {
                    record.CheckStatus = "安检通过";
                }
                else
                {
                    record.CheckStatus = "安检未通过";
               }
            
            

            return record;
        }
    }
}
